package neu.soft.service.impl;

import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.GoodsTransferOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.feignclient.FindInboundRoomId;
import neu.soft.mapper.GoodsTransferOrderMapper;
import neu.soft.message.OutputSource;
import neu.soft.query.WarehousePageQuery;
import neu.soft.service.GoodsTransferOrderService;
import neu.soft.util.RegionCodeUtil;
import neu.soft.vo.MsgBean;
import neu.soft.vo.ResultBean;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class GoodsTransferOrderServiceImpl implements GoodsTransferOrderService {
    @Autowired
    GoodsTransferOrderMapper goodsTransferOrderMapper;
    @Autowired
    private OutputSource source;
    @Autowired
    private FindInboundRoomId findInboundRoomId;

    public <T> boolean sendObject(T msg, Integer delay) throws Exception {
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, delay)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        return source.sendGoods().send(message);
    }

    @Override
    public ResultBean autoCreateGoodsTransferOrder(Map<String,Object> map) throws Exception {
        ResultBean re = new ResultBean();
        List<GoodsTransferOrder> gto=new ArrayList<>();
        List<GoodsTransferOrder> gto1=new ArrayList<>();
        gto1=goodsTransferOrderMapper.findNewOrder(map);
        gto = goodsTransferOrderMapper.findOrder(map);
        gto.addAll(gto1);
        List<GoodsTransferOrder> goodsTransferOrders = new ArrayList<GoodsTransferOrder>();
        for (GoodsTransferOrder groq : gto) {
            HashMap<String, Object> tempmap = new HashMap();
            tempmap.put("oid", groq.getOrderId());
            tempmap.put("commodityId", groq.getCommodityId());
            int temp = goodsTransferOrderMapper.queryExistGoodsTransferOrder(tempmap);
            if (temp != 0) {
                break;
            }
            Date date = new Date();
            long time = date.getTime();
            date.setTime(time + 1000 * 60 * 60 * 24);
            groq.setOutboundTime(date);
            groq.setCompany("中通快递");
            groq.setOutboundRoomId(1);
            String loc=goodsTransferOrderMapper.findLoc(groq.getOrderId());
            Map<String ,String> locSplit = RegionCodeUtil.splitLoc(loc);;
            groq.setInboundRoomId(this.findIdByProvince(locSplit.get("provinceName")));
            goodsTransferOrderMapper.createGoodsTransferOrder(groq);
            this.sendObject(new MsgBean(groq, "货物调拨单创建成功"), 0);
            re = ResultBean.success();
            re.setMsg("创建成功");
        }
        if (re.getCode() == 200) {
            re.setData(goodsTransferOrders);

        }
        return re;
    }

    @Override
    public ResultBean createGoodsTransferOrder(Map<String, Object> map) throws Exception {
        ResultBean re = new ResultBean();
        List<GoodsTransferOrder> gto=new ArrayList<>();
        List<GoodsTransferOrder> gto1=new ArrayList<>();
        gto1=goodsTransferOrderMapper.findNewOrder(map);
        gto = goodsTransferOrderMapper.findOrder(map);
        gto.addAll(gto1);
        String company = (String) map.get("company");
        Integer inboundRoomId = this.findIdByName((String) map.get("inboundRoomId"));
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        String s = map.get("outboundTime").toString();
        Date date = ft.parse(s);
        gto.forEach(groq -> {
            groq.setOutboundTime(date);
            groq.setInboundRoomId(inboundRoomId);
            groq.setCompany(company);
            goodsTransferOrderMapper.createGoodsTransferOrder(groq);
        });
        String orderId = (String) map.get("oid");
        //goodsTransferOrderMapper.batchGoodsTransferOrder(gto);
        List<GoodsTransferOrder> goodsTransferOrderList = goodsTransferOrderMapper.find(orderId);
        if (goodsTransferOrderList == null || goodsTransferOrderList.size() > gto.size()) {
            throw new AppException(AppExceptionCodeMsg.QUERY_FAIL);
        }
        gto.forEach(groq -> {
            try {
                this.sendObject(new MsgBean(groq, "货物调拨单创建成功"), 0);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        return ResultBean.success(goodsTransferOrderList);
    }

    private int findIdByName(String warehouseName){
        WarehousePageQuery warehouseQuery=new WarehousePageQuery();
        warehouseQuery.setWarehouseName(warehouseName);
        return findInboundRoomId.findByPage(warehouseQuery).getData().getRowData().get(0).getWarehouseId();
    }
    private int findIdByProvince(String warehouseAddress){
        WarehousePageQuery warehouseQuery=new WarehousePageQuery();
        warehouseQuery.setWarehouseAddress(warehouseAddress);
        return findInboundRoomId.findByPage(warehouseQuery).getData().getRowData().get(0).getWarehouseId();
    }

}
